const promise = require("./promise.js");
//判断是否授权
function checkAuth() {
  wx.getSetting({
    success: function (res) {
      if (res.authSetting['scope.userInfo']) return true;
      return false;
    }, fail: function () {
      return false;
    }
  })
}
//获取OPENID
function getOpenId() {
  var info = wx.getStorageSync('W_Userinfo');
  var userinfo = info ? JSON.parse(info) : {};
  if (!userinfo.open_id) return 0;
  return userinfo.openid;
}
//获取用户信息
function getUserInfo() {
  var info = wx.getStorageSync('W_Userinfo');
  var userinfo = info ? JSON.parse(info) : {};
  return userinfo;
}
//获取用户token
function getToken() {
  var info = wx.getStorageSync('W_Userinfo');
  var userinfo = info ? JSON.parse(info) : {};
  if (!userinfo.token) return '';
  return userinfo.token;
}
//检查是否授权登陆了  不使用微信的 checksession来判断 type 等于true 的时候代表需要 SETDATA
function checkAuthLogin() {
  var info = wx.getStorageSync('W_Userinfo');
  var userinfo = info ? JSON.parse(info) : {};
  if (!userinfo.openid) return false;
  return true;
}
//提示信息
function showModal(msg) {
  wx.showModal({
    title: '提示', content: msg, showCancel: false
  })
}
// 上传图片
function Wxupload(api, filePath, param, callback) {
  var apiurl = module.exports.globalData.apiurl + api + '?app_id=' + module.exports.globalData.app_id
    + '&app_key=' + module.exports.globalData.app_key,
    that = this;
  var token = getToken();//获取TOKEN
  return new Promise(function (resolve, reject) {
    wx.uploadFile({
      url: apiurl, //仅为示例，非真实的接口地址
      header: { 'User-Token': token },
      name: param,
      filePath: filePath,
      formData: { 'param_name': param },
      success: function (data1) {
        if (that.employIdCallback) {
          that.employIdCallback(1);
        }
        let data = JSON.parse(data1.data);
        switch (data.status) {
          case 200:
            resolve(data.data); break;
          case 400:
            showModal(data.msg); break;
          default:
            showModal(data.msg); break;
        }

      }, fail: function (data) {
        showModal('网络请求失败');
      }
    })
  });
    
}
//POST请求
function Post(api, params, callback) {
  if (!module.exports.globalData.apiurl) {
    wx.getExtConfig({
      success: function (res) {
        module.exports.globalData = res.extConfig;
        WxPost(api, params, callback);
      }
    })
  } else {
    WxPost(api, params, callback);
  }
}
//发起POST请求
function WxPost(api, params, callback) {
  //拼接URL地址
  var apiurl = module.exports.globalData.apiurl + api + '?app_id=' + module.exports.globalData.app_id
    + '&app_key=' + module.exports.globalData.app_key;
  var token = getToken();//获取TOKEN
  wx.request({
    url: apiurl, data: params, header: { 'User-Token': token }, method: 'POST', dataType: 'json',
    success: function (data) {
      switch (data.data.status) {
        case 200:
          callback(data.data.data); break;
        case 400:
          showModal(data.data.msg); break;
        default:
          showModal(data.data.msg); break;
      }
    }, fail: function (data) {
      showModal('网络请求失败');
    }
  })
  wx.hideLoading();
}
//注册函数
module.exports = {
  //login: login,
  Post: Post,
  globalData: [],
  checkAuthLogin: checkAuthLogin,
  getOpenId: getOpenId,
  getUserInfo: getUserInfo,
  getToken: getToken,
  Wxupload: Wxupload
}